<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\controller\Backend;

/**
 * 会员余额变动管理
 *
 * @icon fa fa-circle-o
 */
class Usermoney extends Backend
{
    
    /**
     * Usermoney模型对象
     * @var \app\admin\model\Usermoney
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Usermoney;
        $this->view->assign("moneyTypeList", $this->model->getMoneyTypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 分润
     */
    public function index()
    {
        $UserModel=new User();
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where_new=' usermoney.money_type in("1","2","3","13")';
            $total = $this->model
                    ->with(['user','shoporder'])
                    ->where($where)
                    ->where($where_new)
                    ->order($sort, $order)
                    ->count();
            $MoneyFeel = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->sum('usermoney.money');

            $list = $this->model
                    ->with(['user','shoporder'])
                    ->where($where)
                    ->where($where_new)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $key=> $row) {
//                $row->visible(['money','before','after','memo','createtime','money_type','id']);
//                $row->visible(['user']);
//				$row->getRelation('user')->visible(['username']);
//				$row->visible(['shoporder']);
//				$row->getRelation('shoporder')->visible(['order_no','shop_name']);
                $user=$UserModel::get(['id'=>$row['shoporder']['user_id']]);

                if(!empty($user->username)){
                    $list[$key]['region_name']=$user->username;
                }else{
                    $list[$key]['region_name']='';
                }

                $user=$UserModel::get(['id'=>$row['shoporder']['playUser_id']]);

                if(!empty($user->username)){
                    $list[$key]['region_name_play']=$user->username;
                }else{
                    $list[$key]['region_name_play']='';
                }

            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list,'MoneyFeel'=>$MoneyFeel);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 贡献值记录
     */
    public function agent()
    {
        $UserModel=new User();
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where_new['money_type ']=array('eq',"11");
            $where_new['user.id']=array('neq',0);
            $total = $this->model
                ->with(['user'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->count();

            //统计总的变更贡献值
            $MoneyFeel = $this->model
                ->with(['user'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->sum('usermoney.money');

            $list = $this->model
                ->with(['user'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $key=>$row) {
                $row->getRelation('user')->visible(['username']);
                $user=$UserModel::get(['id'=>$row['shoporder_id']]);

                if(!empty($user->username)){
                    $list[$key]['region_name']=$user->username;
                }else{
                    $list[$key]['region_name']='';
                }

            }




            foreach ($list as $key=>$row){
                if($row['shoporder_id']){

                }

            }
            $result = array("total" => $total, "rows" => $list,'MoneyFeel'=>$MoneyFeel);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 贡献值记录
     */
    public function contvalue()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where_new=' usermoney.money_type ="8"';
            $total = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->count();

            //统计总的变更贡献值
            $MoneyFeel = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->sum('usermoney.money');

            $list = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
                $row->visible(['money','before','after','memo','createtime','money_type','id']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['username']);
                $row->visible(['shoporder']);
                $row->getRelation('shoporder')->visible(['order_no','shop_name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list,'MoneyFeel'=>$MoneyFeel);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 分红记录
     */
    public function bonus()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where_new=' usermoney.money_type ="4"';
            $total = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->count();

            //统计总的变更分红
            $MoneyFeel = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->sum('usermoney.money');

            $list = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
                $row->visible(['money','before','after','memo','createtime','money_type','id']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['username']);
                $row->visible(['shoporder']);
                $row->getRelation('shoporder')->visible(['order_no','shop_name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list,'MoneyFeel'=>$MoneyFeel);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 股权记录
     */
    public function stock()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where_new=' usermoney.money_type ="5" ';
            $total = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->count();

            //统计总的变更股份
            $MoneyFeel = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->sum('usermoney.money');

            $list = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
                $row->visible(['money','before','after','memo','createtime','money_type','id']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['username']);
                $row->visible(['shoporder']);
                $row->getRelation('shoporder')->visible(['order_no','shop_name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list,'MoneyFeel'=>$MoneyFeel);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 提现记录
     */
    public function deposit()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where_new=' usermoney.money_type ="7"';
            $total = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['user','shoporder'])
                ->where($where)
                ->where($where_new)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
                $row->visible(['money','before','after','memo','createtime','money_type','id']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['username']);
                $row->visible(['shoporder']);
                $row->getRelation('shoporder')->visible(['order_no','shop_name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


}
